package com.example.job_backend.controller;

import com.alibaba.excel.EasyExcel;
import com.example.job_backend.model.Query.ScoreQuery;
import com.example.job_backend.model.R;
import com.example.job_backend.model.Score;
import com.example.job_backend.service.ScoreService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ScoreController {
    @Resource
    private ScoreService scoreService;

    @GetMapping("/scores")
    public R getScoreByPage(ScoreQuery query) {
        PageInfo<Score> list = scoreService.getScoreByPage(query);
        return new R(200, "请求成功", list);
    }

    @GetMapping("/scoresWxByTeacher")
    public R getScoresByTeacher(){
        List<Score> list=scoreService.list();
        return new R(200,"请求成功",list);
    }

    @GetMapping("/scoresWxByusername")
    public R getAllScores(String username) {
        List<Score> list = scoreService.getScoreByUsername(username);
        return new R(200, "请求成功", list);
    }

    @PostMapping("/importScores")
    public R importScore(@RequestParam("file") MultipartFile file) throws IOException {
        List<Score> scores = EasyExcel.read(file.getInputStream())
                .head(Score.class)
                .sheet()
                .doReadSync();
        for (Score score : scores) {
            scoreService.save(score);
        }
        return new R(200, "导入成功", null);
    }

    @GetMapping("/deleteScore/{id}")
    public R deleteScore(@PathVariable("id") Integer id) {
        scoreService.removeById(id);
        return new R(200, "删除成功", null);
    }

    @GetMapping("/updateScore")
    public R updateScore(Score score) {
        scoreService.updateById(score);
        return new R(200, "更新成功", null);
    }

    @GetMapping("/addScore")
    public R addScore(Score score) {
        scoreService.save(score);
        return new R(200, "添加成功", null);
    }
}
